package com.chj.leetcode.tijie200to250;

import java.util.ArrayDeque;
import java.util.Deque;

public class Code225_ImplementStackUsingQueues {
	// 队列实现栈
	public class TwoQueues_Stack {
		Deque<Integer> queue = new ArrayDeque<>();
		Deque<Integer> helper = new ArrayDeque<>();

		public void push(int pushInt) {
			queue.add(pushInt);
		}

		public int pop() {
//			if (queue.isEmpty()) {
//				throw new RuntimeException("Stack is empty!");
//			}
//			while (queue.size() != 1) {
//				helper.add(queue.poll());
//			}
//			int res = queue.poll();
			int res = pp();
			swap();

			return res;
		}

		public int peek() {
//			if (queue.isEmpty()) {
//				throw new RuntimeException("Stack is empty!");
//			}
//			while (queue.size() != 1) {
//				helper.add(queue.poll());
//			}
//			int res = queue.poll();
			int res = pp();
			helper.add(res);
			swap();

			return res;
		}

		public void swap() {
			Deque<Integer> temp = helper;
			helper = queue;
			queue = temp;
		}

		private int pp() {
			if (queue.isEmpty()) {
				return -1;
			}
			while (queue.size() != 1) {
				helper.add(queue.poll());
			}
			int res = queue.poll();
			return res;
		}
	}
}
